'use strict'

const crypto = require('crypto')

const algorithm = 'aes-256-cbc'
const key = 'AES_KEY'
const iv = 'AES_IV'

const content = {
  "msgId": "<Message ID, it's a UUID string>",
  "cmd": "ADD_GW_RES",
  "gwId": "<Gateway ID>",
  "uid": "<User ID>",
  "isConfigured": true
}
const contentString = JSON.stringify(content)

// 加密

let cipherChunks = []
const cipher = crypto.createCipheriv(algorithm, key, iv)
cipher.setAutoPadding(true)

cipherChunks.push(cipher.update(contentString, 'utf8', 'base64'))
cipherChunks.push(cipher.final('base64'))

const secretData = cipherChunks.join('')
console.log('secretData: ' + secretData)

// 解密

let decipherChunks = []
const decipher = crypto.createDecipheriv(algorithm, key, iv)
decipher.setAutoPadding(true)

decipherChunks.push(decipher.update(secretData, 'base64', 'utf8'))
decipherChunks.push(decipher.final('utf8'))

const data = decipherChunks.join('')
console.log(`data: `, data)
